<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>协会概况</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/manage/layui/css/layui.css" media="all">
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
</head>
<body>
<div class="x-nav">
        <span class="nav-breadcrumb">
            <a><cite>后台管理></cite></a>
            <a><cite>网站首页></cite></a>
            <a><cite>药品信息</cite></a>
        </span>
</div>
<div class="x-body">
    <blockquote class="layui-elem-quote quoteBox">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" class="layui-input searchContent" placeholder="请输入关键字" />
            </div>
            <a class="layui-btn search_btn" data-type="reload">搜索</a>
        </div>
        <div class="layui-inline">
            <a class="layui-btn  addNews_btn">添加</a>
        </div>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </blockquote>
    <table id="DrugsTable" lay-filter="DrugsTable"></table>
</div>
</body>
</html>
<script type="text/javascript">
    layui.use(['jquery', 'table', 'layer', 'util'], function() {
        var $ = layui.$, table = layui.table, layer = layui.layer, util = layui.util;
        var active = {
            reload: function(){
                table.reload('search_table_id', {
                    where: {
                        name: $('.searchContent').val(),
                        page: 1
                    }
                });
            }
        };
        table.render({
            elem: '#DrugsTable'
            , height: 500
            , url: 'Drugs/DrugsTable'
            ,id: 'search_table_id'
            , page: true
            , cols: [[
                {field: 'id', title: 'ID', width: 150, sort: true, fixed: true},
                {field: 'name', title: '药品名称', width: 150, sort: true},
                {field: 'action', title: '作用', width: 150},
                {field: 'typeName', title: '药品类型', width: 150},
                {field: 'buytime', title: '购买时间', width: 150},
                {field: 'selltime', title: '售出时间', width: 150},
                {field: 'stock', title: '库存', width: 150},
                {
                    field: 'tempId', title: '操作', width: 240, templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="read" lay-id="' + d.tempId + '"><i class="layui-icon layui-icon-read"></i>查看</a>' +
                            '<a class="layui-btn layui-btn-xs" lay-event="edit" lay-id="' + d.tempId + '"><i class="layui-icon layui-icon-edit"></i>编辑</a>'+
                            '<a class="layui-btn layui-btn-xs" lay-event="del" lay-id="' + d.tempId + '"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                    }
                }
            ]]
        });
        // 搜索（利用定义的active重新载入表格）
        $('.search_btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        // 监听工具条
        table.on('tool(DrugsTable)', function (obj) {
            // data当前行数据， lay-event或event 对应的值，当前行tr的DOM对象
            var data = obj.data, event = obj.event;
            console.log(data);
            switch(event){
                case "read":
                    readYaopin(data);
                    break;
                case "edit":
                    updateYaopin(data);
                    break;
                case "del":
                    layer.confirm('真的删除行么？', function(index){
                        layer.close(index);
                        $.ajax({
                            type: 'post',
                            url: '/manage/Drugs/deleteDrugs',
                            data: {'id': data.id},
                            success:function(result){
                                alert(result);
                                window.location.reload();      // 刷新
                            }
                        });
                    });
                    break;
            }
        })
        // 查看
        function readYaopin(rowData) {
            layer.open({
                title : "查看",
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['500px', '400px'],
                content: "/manage/Drugs/toReadDrugs/" + rowData.id
            });
        }
        // 编辑
        function updateYaopin(rowData) {
            layer.open({
                title : "编辑",
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['500px', '400px'],
                content: "/manage/Drugs/toUpdateDrugs/" + rowData.id
            });
        }
        $('.addNews_btn').on('click', function(){
            layer.open({
                title : "添加首页内容",
                type : 2,
                fix: false, //不固定
                maxmin: true,
                area: ['500px', '400px'],
                content: "/manage/Drugs/toAddYaopin",
                success: function (layero, index) {
                    // 动态生成文章 下拉框
                    var body = layui.layer.getChildFrame('body', index);
                }
            });
        });
    })
</script>

